define(function (require, exports, module) {
    'use strict';
    var $ = require('jquery'),
        _ = require('underscore'),
        Backbone = require('backbone'),
        notify = require('js/utils/notify'),
        models = require('js/apps/op/models'),
        utils = require('js/utils/tools'),
        uploader = require('js/utils/uploader'),
        app = Backbone.history;

    var ABTestListView = Backbone.View.extend({
        tagName: "div",
        events: {
            'click a.delete-item': 'toDelete',
        },

        initialize: function (options) {
            this.options = options;
            this.collection = new models.ABTestCollection();
            this.collection.bind('change reset remove', this.renderWithData, this);
        },
        render: function () {
            this.$el.html("");
            $(window).scrollTop(0);
            return this;
        },
        toDelete: function (e) {
            utils.deleteItem(e, this.collection);
        },
        renderWithData: function () {
            this.$el.html(this.template({
                items: this.collection.toJSON(),
            }));
            var that = this;
            utils.renderTable('main-list', {
                $orderby: that.options.$orderby || 'id',
                sortCallback: function (field) {
                    that.options.$orderby = field;
                    that.options.$page = 1;
                    var newUrl = utils.composeQueryString('#op/abtest/', that.options);
                    app.navigate(newUrl, {
                        trigger: true
                    });
                }
            });
            utils.getPaginator(that.options, this.collection.total, '#op/abtest/');

            return this;
        },
        load: function () {
            this.collection.fetch({
                reset: true,
                data: this.options,
                error: function (c, r, o) {
                    notify.notifyResp(r);
                },
            });
        }
    });
    exports.List = Backbone.View.extend({
        el: "#content_wrapper",
        template: "op/ABTestList.html",

        initialize: function (options) {
            this.options = options;
        },
        render: function () {
            var view = new ABTestListView(this.options);
            view.template = this.template;

            this.$el.empty();
            this.$el.append(view.render().el);
            view.load();

            return this;
        }
    });
    var ABTestDetailView = Backbone.View.extend({
        tagName: "div",
        className: "panel-body pn",
        events: {
            'click #save': 'toSaveOrCreate',
            'click .switch-key': 'switchKey',
            'click .switch-key-cvc': 'switchKeyCvc',
            'click .shortcut': 'quickAdd'
        },
        initialize: function (options) {
            this.model = new models.ABTest();
            if (options.id) {
                this.model.set('id', options.id);
            }
            this.model.bind('change reset', this.renderWithData, this);
        },
        render: function () {
            this.$el.html("");
            $(window).scrollTop(0);
            return this;
        },
        switchKey: function (e) {
            var $this = $(e.target);
            if ($this.data('key') == 'include') {
                $this.data('key', 'exclude').html('排除渠道');
            } else {
                $this.data('key', 'include').html('包含渠道');
            }
            return false;
        },
        switchKeyCvc: function (e) {
            var $this = $(e.target);
            if ($this.data('key') == 'include') {
                $this.data('key', 'exclude').html('排除版本');
            } else {
                $this.data('key', 'include').html('包含版本');
            }
            return false;
        },
        quickAdd: function (e) {
            e.preventDefault();
            var $this = $(e.target),
                aria = $this.data('aria'),
                $pel = $this.closest('.input-group'),
                $key = $('#inputKey'),
                $el = $('#inputChannel');
            switch (aria) {
                case 'allAndroid':
                    $key.data('key', 'exclude').html('排除渠道');
                    $el.val(NOT_ANDROID_CHN.join(','));
                    break;
                case 'allIOS':
                    $key.data('key', 'include').html('包含渠道');
                    $el.val(IOS_CHN.join(','));
                    break;
                case 'allQG':
                    $key.data('key', 'include').html('包含渠道');
                    $el.val(IOS_QG_CHN.join(','));
                    break;
            }
            $pel.find('[data-toggle="dropdown"]').parent().removeClass('open');
            return false;
        },
        toSaveOrCreate: function (e) {
            e.preventDefault();
            var attrs = {
                    user_id: [$('#inputUserIdLower').val(), $('#inputUserIdUpper').val()],
                },
                chn = $('#inputChannel').val(),
                cvc = $('#inputCvc').val();
            if (chn.length > 0) {
                attrs.chn = {}
                var key = $('#inputKey').data('key');
                attrs.chn[key] = chn.split(',');
            }
            if (cvc.length > 0) {
                attrs.cvc = {}
                var key = $('#inputCvcKey').data('key');
                attrs.cvc[key] = cvc.split(',');
            }

            var saveData = {
                content: JSON.stringify(attrs),
                name: $('#inputName').val()
            }
            this.model.save(saveData, {
                patch: true,
                success: function (model, response) {
                    notify.success('提示', '保存成功！');
                    setTimeout(function () {
                        history.back();
                    }, 500);
                },
                error: function (model, response) {
                    notify.notifyResp(response);
                }
            });

            return false;
        },
        renderWithData: function () {
            this.$el.html(this.template({
                info: this.model.toJSON()
            }));
            return this;
        },
        load: function () {
            if (this.model.id) {
                this.model.fetch({
                    reset: true
                });
            } else {
                this.renderWithData();
            }
        }
    });
    exports.Detail = Backbone.View.extend({
        el: "#content_wrapper",
        template: "op/ABTestDetail.html",

        initialize: function (options) {
            this.options = options;
        },
        render: function () {
            var view = new ABTestDetailView(this.options);
            view.template = this.template;

            this.$el.empty();
            this.$el.append(view.render().el);
            view.load();

            return this;
        }
    });
});
